<?php
/**
* This example generates an AFC aggregate report.
*
* PHP version 5
* PHP extensions: SoapClient and OpenSSL.
*
* Copyright 2011, Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @package    GoogleApiAdsAdSense
* @subpackage v3
* @category   WebServices
* @copyright  2011, Google Inc. All Rights Reserved.
* @license    http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
* @author     Sergio Gomes <api.sgomes@gmail.com>
* @link       http://code.google.com/apis/adsense/developer/ReportService.html#generateReport
*/

error_reporting(E_STRICT | E_ALL);

// Provide AdSense login information.
$developer_email = 'INSERT_DEVELOPER_EMAIL_HERE';
$developer_password = 'INSERT_DEVELOPER_PASSWORD_HERE';
$display_locale = 'en_US';
$client_id = 'INSERT_PUBLISHER_ID_HERE';

// Set SOAP and XML settings. To send requests to the production environment,
// replace "sandbox.google.com" with "www.google.com" in the wsdl URL.
// The namespace will always be "www.google.com", even in the sandbox.
$wsdl = 'https://sandbox.google.com/api/adsense/v3/ReportService?wsdl';
$namespace = 'http://www.google.com/api/adsense/v3';
$options = array(
    'features' => SOAP_SINGLE_ELEMENT_ARRAYS,
    'encoding' => 'utf-8',
    'compression' => SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP,
    'user_agent' => 'PHP-SOAP/'. phpversion() . ',gzip',
    // We're setting WSDL caching so that we don't hammer the servers retrieving
    // the WSDL for every run. If caching causes any odd behaviour for you, set
    // this to WSDL_CACHE_NONE, download the WSDL to your disk, and set $wsdl to
    // the file's location.
    'cache_wsdl' => WSDL_CACHE_BOTH);

// Get ReportService.
$report_service = new SoapClient($wsdl, $options);

// Set headers.
$developer_email_header = new SoapHeader($namespace, 'developer_email',
    $developer_email);
$developer_password_header = new SoapHeader($namespace, 'developer_password',
    $developer_password);
$display_locale_header = new SoapHeader($namespace, 'display_locale',
    $display_locale);
$client_id_header = new SoapHeader($namespace, 'client_id', $client_id);
$report_service->__setSoapHeaders(array($developer_email_header,
    $developer_password_header, $display_locale_header, $client_id_header));

// Generate report.
try {
  $report_data = new stdClass();
  $report_data->dateRange = new stdClass();
  $report_data->dateRange->isPredefined = true;
  $report_data->dateRange->predefinedDateRange = new stdClass();
  $report_data->dateRange->predefinedDateRange->value = 'Yesterday';
  $report_data->outputFormat = new stdClass();
  $report_data->outputFormat->value = 'CSV';
  $report_data->unit = new stdClass();
  $report_data->unit->value = 'Page';
  $params = new stdClass();
  $params->report = new SoapVar(
      $report_data, SOAP_ENC_OBJECT, 'ns:AFCAggregateReport');
  $result = $report_service->generateReport($params);
} catch (SoapFault $e) {
  print $report_service->__getLastRequest();
  if (isset($e->detail) && isset($e->detail->AdSenseApiExceptionFault) &&
      isset($e->detail->AdSenseApiExceptionFault->AdSenseApiException)) {
    // Caught AdSense API exception.
    $inner = $e->detail->AdSenseApiExceptionFault->AdSenseApiException;
    print "An error occurred while generating report:\n";
    print $inner->message;
    print "\n";
  } else {
    // Caught other type of exception.
    print "Unexpected error while generating report.\n";
    print $e->getMessage() . "\n";
  }
  exit(1);
}

// Display report data.
if (isset($result->return)) {
  if($result->return->rowCount > 0)
    print "Generated report:\n" . $result->return->data . "\n";
  else
    print "The generated report is empty.\n";
}
